//
//  DemoController.m
//  MClient
//
//  Created by Zhouyi on 10-8-20.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "DemoController.h"
#import "MServerDAO.h"
#import "MServerConfigCache.h"
#import "MServerJsonDAO.h"
#import "MenuController.h"
#import "MCTableCell.h"
#import "MCTabBarController.h"
#import "loginStat.h"
#import "UIDevice+IdentifierAddition.h"
@interface DemoController (Private)
- (void)initDemoMenus;
- (void)doLogin;
@end

@implementation DemoController

@synthesize demoMenus;
@synthesize partyId;
@synthesize userId;
@synthesize password;
@synthesize serverUrl;
@synthesize demoTitle;

#pragma mark ---- 类基本方法 ----

- (int)identiferId {
	return 0;
}
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
	[super viewDidLoad];
	
	self.title = @"应用演示";

	// tableview背景透明
	self.tableview.backgroundColor = [UIColor clearColor];
	
	[self initDemoMenus];
	statJsonDao = [[loginStat alloc] init];
}

- (void)dealloc {
	[demoMenus release];
	[partyId release];
	[userId release];
	[password release];
	[serverUrl release];
	[statJsonDao release];
	[demoTitle release]; 
    [super dealloc];
}

#pragma mark ---- UITableView方法 ----

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	return [self.demoMenus count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	MenuCell *cell = (MenuCell*)[super tableCell:tableView withClass:[MenuCell class] withIdentifier:@"MenuTableCell"];
	
	NSMutableArray *demoMenu = [self.demoMenus objectAtIndex:indexPath.row];
	NSString *imageFile = [demoMenu objectAtIndex:0];
	if (imageFile == nil || [imageFile length] == 0) {
		imageFile = @"0.png";
	}
	
	[cell showCell:[@"   " stringByAppendingString:[demoMenu objectAtIndex:1]]
	      withIcon:imageFile
			  menu:[NSMutableDictionary dictionary]];
	
	return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	NSUInteger row = [indexPath row];
	NSMutableArray *demoMenu = [self.demoMenus objectAtIndex:row];
	self.demoTitle = [demoMenu objectAtIndex:1];
	self.partyId = [demoMenu objectAtIndex:2];
	self.userId = [demoMenu objectAtIndex:3];
	self.password = [demoMenu objectAtIndex:4];
	[self doLogin];
	[self performSelector:@selector(deselect) withObject:nil afterDelay:0.5f];
}

- (void) deselect {
	[self.tableview deselectRowAtIndexPath:[self.tableview indexPathForSelectedRow] animated:YES];
}

- (void)doLogin {

	[MServerJsonDAO setServerServletUrl:self.serverUrl];
	[[MServerDAO sharedInstance] loginAsync:partyId
									 userId:userId
								   password:password
									   imei:[[UIDevice currentDevice] uniqueGlobalDeviceIdentifier]
									   imsi:@""
									  acode:@""
								   delegate:self];
}

- (void)getDataFromServerSuccess: (NSMutableDictionary *)returnValue {

	statJsonDao.partyId = partyId;
	statJsonDao.userId = userId;
	statJsonDao.serverUrl = @"http://demo.mobile086.cn";
	[statJsonDao sendStatRequest];
	
	NSMutableDictionary *loginReturn = [returnValue retain];
	
	NSString *timeStamp = [loginReturn objectForKey:@"ldt"];
	
	[[MServerConfigCache sharedInstance] loadCacheFromStore:timeStamp server:self.serverUrl party:partyId user:userId];
	
	if ([returnValue objectForKey:@"sct"] && [[returnValue objectForKey:@"sct"] count] > 0) {
		MCTabBarController* tabBarController = [[MCTabBarController alloc] init];
		tabBarController.tabBarData = returnValue;
		[self.navigationController pushViewController:tabBarController animated:YES];
		[tabBarController release];
	} else {
		MenuController *menuViewController = [[MenuController alloc] init];
		menuViewController.title = demoTitle;
		menuViewController.menus = [loginReturn objectForKey:@"app"];
		menuViewController.isApp = YES;
		
		menuViewController.navigationItem.hidesBackButton = YES;
		[self.navigationController pushViewController:menuViewController animated:YES];
		[menuViewController release];
	}
	
	[loginReturn release];
}

#pragma mark ---- 内部方法 ----

- (void)initDemoMenus {
	self.serverUrl = @"http://demo.mobile086.cn/beapp/MobileCommunicationServletV4";
	self.demoMenus = [NSMutableArray array];
	NSArray *demoMenu = [NSArray arrayWithObjects:@"ydbg.png",@"移动办公",@"moa",@"admin",@"111111",nil];
	[self.demoMenus addObject:demoMenu];
	demoMenu = [NSArray arrayWithObjects:@"ydds.png",@"移动定损",@"lsc",@"admin",@"111111",nil];
	[self.demoMenus addObject:demoMenu];
	demoMenu = [NSArray arrayWithObjects:@"ydcrm.png",@"移动CRM",@"mcrm",@"admin",@"111111",nil];
	[self.demoMenus addObject:demoMenu];
	demoMenu = [NSArray arrayWithObjects:@"ydsw.png",@"移动税务",@"mtax",@"admin",@"111111",nil];
	[self.demoMenus addObject:demoMenu];
	demoMenu = [NSArray arrayWithObjects:@"ydhg.png",@"移动海关",@"mhg",@"admin",@"111111",nil];
	[self.demoMenus addObject:demoMenu];
	demoMenu = [NSArray arrayWithObjects:@"ydsp.png",@"移动审批",@"anypass",@"admin",@"111111",nil];
	[self.demoMenus addObject:demoMenu];
	demoMenu = [NSArray arrayWithObjects:@"cwt.png",@"车务通",@"vehiclem",@"admin",@"111111",nil];
	[self.demoMenus addObject:demoMenu];
	demoMenu = [NSArray arrayWithObjects:@"cgt.png",@"城管通",@"city_manager",@"admin",@"111111",nil];
	[self.demoMenus addObject:demoMenu];
	demoMenu = [NSArray arrayWithObjects:@"ydkxt.png",@"移动快销通",@"fps",@"admin",@"111111",nil];
	[self.demoMenus addObject:demoMenu];
	demoMenu = [NSArray arrayWithObjects:@"kqgl.png",@"考勤管理",@"timecard",@"admin",@"111111",nil];
	[self.demoMenus addObject:demoMenu];
	demoMenu = [NSArray arrayWithObjects:@"wygl.png",@"物业管理",@"emstdemo",@"admin",@"111111",nil];
	[self.demoMenus addObject:demoMenu];
	demoMenu = [NSArray arrayWithObjects:@"fczj.png",@"房产中介",@"himtdemo",@"admin",@"111111",nil];
	[self.demoMenus addObject:demoMenu];
	demoMenu = [NSArray arrayWithObjects:@"cygl.png",@"餐饮管理",@"eatedemo",@"admin",@"111111",nil];
	[self.demoMenus addObject:demoMenu];
	demoMenu = [NSArray arrayWithObjects:@"dhxt.png",@"订货系统",@"qingmei",@"admin",@"111111",nil];
	[self.demoMenus addObject:demoMenu];
	demoMenu = [NSArray arrayWithObjects:@"sjyh.png",@"手机银行",@"spdbank",@"admin",@"111111",nil];
	[self.demoMenus addObject:demoMenu];
}

@end
